package com.hh.storeArea1.service.impl;

import com.hh.commen.bean.Common;
import com.hh.store.VO.StoreForQueryWebVo;
import com.hh.storeArea1.Vo.*;
import com.hh.storeArea1.mapper.StoreAreaHhMapper;
import com.hh.storeArea1.service.StoreAreaHhService;
import com.hh.storeRoom.mapper.StoreRoomMapper;
import com.hh.utils.PageVo;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hanh
 * @date 2023/8/17 16:17s
 */
@Service
public class StoreAreaHhServiceImpl implements StoreAreaHhService {

    @Autowired
    private StoreAreaHhMapper storeAreaHhMapper;

    @Autowired
    private StoreRoomMapper storeRoomMapper;

    @Override
    public ResultVo queryStorePage(QueryStoreAreaPageVo queryStorePageVo) {
        //计算总条数
        //1.计算总的数据量
        Integer totalNumber = storeAreaHhMapper.getAllStoreCount(queryStorePageVo);
        //2.计算偏移量
        queryStorePageVo.setOffsize((queryStorePageVo.getPageIndex()-1)* queryStorePageVo.getPageSize());
        //3.获取合适条件的数据
        List<StoreForQueryWebVo> storeList = storeAreaHhMapper.getStoreByQuery(queryStorePageVo);
        //4.计算总页数
        PageVo<StoreForQueryWebVo> pageVo = new PageVo<>();
        Integer totalPage =totalNumber%queryStorePageVo.getPageSize()==0?totalNumber/queryStorePageVo.getPageSize():
                totalNumber/queryStorePageVo.getPageSize()+1;
        pageVo.setTotalNumber(totalNumber);
        pageVo.setPageIndex(queryStorePageVo.getPageIndex());
        pageVo.setDataList(storeList);
        pageVo.setOffSize((queryStorePageVo.getPageIndex()- 1)* queryStorePageVo.getPageSize());
        pageVo.setTotalPage(totalPage);
        return ResultVo.success(pageVo);
    }

    @Override
    public ResultVo addStoreArea(AddStoreAreaVo addStoreAreaVo) {
        //校验库区编码是否重复
        Integer coutExistCode = storeAreaHhMapper.getAreaByCode(addStoreAreaVo.getCode());
        if (coutExistCode>0){
            return ResultVo.error("您所添加的库区编码已存在，请重新添加");
        }
        //校验库区名称是否重复
        Integer coutExistName = storeAreaHhMapper.getAreaByName(addStoreAreaVo.getName());
        if (coutExistName>0){
            return ResultVo.error("您所添加的库区名称已存在，请重新添加");
        }
        //添加库区信息
        storeAreaHhMapper.addArea(addStoreAreaVo);
        //添加库区属性信息
        for (String attribute:addStoreAreaVo.getAttributeCode()){
            storeAreaHhMapper.insertAreaAttribute(addStoreAreaVo.getCode(),attribute);
        }
        return ResultVo.success("插入成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo updateStoreArea(UpdateStoreAreaVo updateStoreAreaVo) {
        //校验库区名称是否重复
        Integer coutExistName = storeAreaHhMapper.getAreaByName(updateStoreAreaVo.getName());
        if (coutExistName>0){
            return ResultVo.error("您所修改的库区名称已存在，请重新添加");
        }
        try {
        //修改库区信息
        storeAreaHhMapper.updateAreaByCode(updateStoreAreaVo);
        //修改属性信息
        //删除原有
        storeAreaHhMapper.removeAreaAttribute(updateStoreAreaVo.getCode());
        //插入新的
        for (String attribute : updateStoreAreaVo.getAttributeCode()){
            storeAreaHhMapper.insertAreaAttribute(updateStoreAreaVo.getCode(),attribute);
        }
        return ResultVo.success("成功修改！");
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.error("修改失败");
        }
    }

    @Override
    public ResultVo updateStatus(UpdateStatusVo statusVo) {
        storeAreaHhMapper.updateStatus(statusVo);
        return ResultVo.success("状态修改完成！");
    }

    @Override
    public ResultVo addStoreAreaForWeb(AddForWebVo2 addStoreAreaVo) {
        try {
            //验证库房是否存在
            Integer coutExistRoomCode = storeRoomMapper.getCountByCode(addStoreAreaVo.getRoomCode());
            if (coutExistRoomCode==0||coutExistRoomCode==null){
                return ResultVo.error("您所选择的库房不存在，请重新添加");
            }
            //校验库区编码是否重复
            Integer coutExistCode = storeAreaHhMapper.getAreaByCode(addStoreAreaVo.getCode());
            if (coutExistCode>0){
                return ResultVo.error("您所添加的库区编码已存在，请重新添加");
            }
            //校验库区名称是否重复
            Integer coutExistName = storeAreaHhMapper.getAreaByName(addStoreAreaVo.getName());
            if (coutExistName>0){
                return ResultVo.error("您所修改的库区名称已存在，请重新添加");
            }
            //校验库区的系统属性至少选择一项
            if (!addStoreAreaVo.getWms()&&!addStoreAreaVo.getVip()&&!addStoreAreaVo.getManage()){
                return ResultVo.error("请您至少选择一项系统属性！");
            }
            //添加库区信息
            storeAreaHhMapper.addAreaForWeb(addStoreAreaVo);
            //添加库区业务属性信息
            storeAreaHhMapper.insertAreaAttribute2(addStoreAreaVo.getCode(),addStoreAreaVo.getAttribute());
            return ResultVo.success("添加成功！");
        }catch (Exception e){
            e.printStackTrace();
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultVo.error("添加失败");
        }
    }

    @Override
    public ResultVo queryAttributeByCode(String code) {
        GetAttributeForWebVo attributeForWebVo = new GetAttributeForWebVo();
        //根据库区code查看它拥有的业务属性
        List<Common> attribute = storeAreaHhMapper.queryAttributeByCode(code);
        List<Common> businessAttributes = new ArrayList<>();
        for(Common common: attribute){
            if (common.getCode().equals("S001")){
                attributeForWebVo.setWarehouseManagement(true);
            }else if (common.getCode().equals("S002")){
                attributeForWebVo.setWmsIntegrate(true);
            } else if (common.getCode().equals("S003")) {
                attributeForWebVo.setVipStorageSection(true);
            }else {
                businessAttributes.add(common);
            }
        }
        attributeForWebVo.setAttribute(businessAttributes);
        return ResultVo.success(attributeForWebVo);
    }

    /**
     * 修改的实质是删除再插入
     * @param addStoreAreaVo
     * @return
     */
    @Override
    public ResultVo updateStoreArea2(UpdateForWebVo2 addStoreAreaVo) {
        //校验库区名称是否重复
        //获取原有名称  只有在修改原有名称时才会进行修改
        String name = storeAreaHhMapper.getAreaNameByCode(addStoreAreaVo.getCode());
        if (!name.equals(addStoreAreaVo.getName())){
            Integer coutExistName = storeAreaHhMapper.getAreaByName(addStoreAreaVo.getName());
            if (coutExistName>0){
                return ResultVo.error("您所修改的库区名称已存在，请重新添加");
            }
        }
        //修改原数据的remak和name,以及原有的管理属性
        storeAreaHhMapper.updateStoreAreaByCode(addStoreAreaVo);
        //删除原有数据的属性集合
        storeAreaHhMapper.removeAreaAttribute(addStoreAreaVo.getCode());
        //执行插入
        storeAreaHhMapper.insertAreaAttribute2(addStoreAreaVo.getCode(),addStoreAreaVo.getAttribute());
        return ResultVo.success("修改成功！");

    }
}


